/**
 * @description  : 全局混入
 * @descriptionDetail : 导航栏设置、返回上页等api
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-10-03 16:24:32
 */

import commonService from '../service/common/common.service';
/**
 * 钉钉导航栏、标题设置等
 */
export const commonMixin = {
	data() {
		return {
			navTitle: "", // 导航栏标题
			isHideNav: this.$deviceApi.isHideNav(), // 仅在开发时显示
			mixinParam: {},
			mixinFlag: false, // 是否触发返回参数逻辑
			showDefaultTitle: true, // 是否展示默认title 
			curUser: {}, // 当前的用户信息
		}
	},
	watch: {
		$router(to, from) {
			this.setRightBtns();
		},
	},
  created() {
    this.curUser = this.$store.state.userInfo;
  },
	onLoad() {
		this.curUser = this.$store.state.userInfo;
	},
	onBackPress(f, o) {
		return this.canBack;
	},
	onShow() {
		// 当前页面路径
		let currPath = this.$route.meta.pagePath;
		console.log("mixin混入", currPath);
		let currRouteCfg = this.$r.filter(p => p.path == currPath);
		// console.log("当前页面路由配置", currRouteCfg);
		if (currRouteCfg && currRouteCfg.length > 0) {
			let ddTitle = (currRouteCfg[0].style || {}).navigationBarTitleText;
			if (this.showDefaultTitle) {
				this.navTitle = ddTitle;
			}
			if (ddTitle && this.$deviceApi.isHideNav()) {
				// 设置钉钉标题
				if (this.showDefaultTitle) {
					this.$deviceApi.setTitle && this.$deviceApi.setTitle(ddTitle);
				}
				this.setRightBtns();
				// 添加功能埋点信息
				this.$deviceApi.buriedPointFunction && this.$deviceApi.buriedPointFunction('点击' + ddTitle);
			}
		}
		if (currRouteCfg && currRouteCfg.length > 0) {
			this.savePageLog(currPath, "", (currRouteCfg[0].style || {}).navigationBarTitleText);
		}
		if (this.mixinFlag) {
			this.updateBackState(this.mixinParam);
			this.mixinFlag = false;
			this.mixinParam = {};
		}
	},
	methods: {
		setRightBtns() {
			// 隐藏导航栏右侧按钮(在有需要的地方再添加)
			// #ifdef H5-DINGDING
			this.$deviceApi.setRight("", () => {}, false, false);
			// #endif
			// 隐藏企业微信右上角菜单
			// #ifdef H5-WECHAT
			this.$deviceApi.hideOptionMenu();
			// #endif
		},
		updateMixinTitle(title) {
			this.navTitle = title;
			this.$deviceApi.setTitle && this.$deviceApi.setTitle(this.navTitle);
		},
		// 返回
		goBack() {
			uni.navigateBack(); // 返回上一页
		},
		// 返回并刷新上一页列表
		goRBack() {
			let pages = getCurrentPages();
			let prePage = pages[pages.length - 2]; // 上一页
			prePage && prePage.$refs['mescrollItem0'] && prePage.$refs['mescrollItem0'].refresh();
			uni.navigateBack(); // 返回上一页
		},

		// 添加回执参数
		setMixinParam: function(key, value) {
			this.mixinParam[key] = value;
		},
		/**
		 * 	paramKey : undefined, // 对应回传参数过度复杂,则使用临时字段名 key
		 *	param : undefined,  // 回传参数值
		 */
		backPrev: function(obj, dail = 1) {
			let pages = getCurrentPages(); // 获取所有页面栈实例列表
			if (pages.length <= dail) {
				this.$modal.alert("不能返回");
				return;
			}
			let nowPage = pages[pages.length - 1]; // 当前页页面实例
			let prevPage = pages[pages.length - (dail + 1)]; // 上一页页面实例
			if (obj) {
				prevPage.$vm.mixinFlag = true;
				prevPage.$vm.setMixinParam(obj.paramKey, obj.param);
			}
			uni.navigateBack({
				delta: dail,
			});
		},
		// 获取默认部门
		getDefaultOrg(parentUuid, treeType, callBack) {
			commonService.orgTree(parentUuid, treeType).then(dr => {
				if (dr.success) {
					let orgs = dr.data;
					if (orgs.length > 0) {
						orgs.sort((a, b) => {
							return a.nodeType - b.nodeType;
						});
						if (orgs.length > 0) {
							callBack && callBack(orgs[0]);
						} else {
							callBack && callBack();
						}
					}
				}
			}).catch(e => {
				callBack && callBack();
			});
		},
		getPageIns: function(dail = 1) {
			let pages = getCurrentPages(); // 获取所有页面栈实例列表
			if (pages.length <= dail) {
				return undefined;
			}
			let prevPage = pages[pages.length - (dail + 1)]; // 上一页页面实例
			return prevPage;
		},
		/**
		 * 触发上一页面的刷新
		 * 应用场景：eg保存当前页面后未返回，调用此方法，在返回页面后处理特殊的逻辑(例如刷新列表页面)
		 * @param {*} dail 
		 * @returns 
		 */
		triggerPrevRefresh(dail = 1) {
			let pages = getCurrentPages(); // 获取所有页面栈实例列表
			if (pages.length <= dail) {
				return;
			}
			let prevPage = pages[pages.length - (dail + 1)]; // 上一页页面实例
			prevPage.$vm.mixinFlag = true;
			prevPage.$vm.setMixinParam("refresh", true);
		},
		/**
		 * 设置路由参数
		 * @param {*} param 
		 */
		setRouteParam(param = "") {
			getApp().globalData.intentParams = param;
		},
		/**
		 * 获取路由参数
		 * @returns 
		 */
		getRouteParam() {
			return getApp().globalData.intentParams;
		},
		objMerge(form, rs) {
			for (let k in rs) {
				if (Object.prototype.hasOwnProperty.call(form, k)) {
					form[k] = rs[k];
				} else {
					this.$set(form, k, rs[k]);
				}
			}
		},
		savePageLog(pagePath = "", prePath = "", moduleName = "", ) {
			let userInfo = this.curUser;
			let ar = [{
				browserInfo: "",
				deptUuid: userInfo.user.org.orgUuid,
				enterTime: Date.now(),
				ip: "0.0.0.0",
				leaveTime: "",
				loginName: userInfo.loginName,
				module: "",
				moduleName: moduleName,
				pageEvent: "",
				personName: userInfo.user.personName,
				platform: "dingding",
				stack: prePath,
				stayTime: "",
				uri: pagePath,
			}, ];
			commonService.savePageLog(ar).then(res => {
				console.log('日志保存=' + JSON.stringify(res));
			}).catch(er => {
				console.log('日志保存异常=' + er);
			});
		},
		/**
		 * 业务埋点
		 * @param {*} operateDesc 操作描述， 如：零售户走访
		 * @param {*} operateInfo 操作内容， 如：零售户许可证编号
		 */
		buriedPointBusiness(operateDesc, operateInfo) {
			this.$deviceApi.buriedPointBusiness && this.$deviceApi.buriedPointBusiness(operateDesc, operateInfo);
		},
	},
}